package   com.stars.home.update.util

import android.app.Activity
import android.content.Context
import androidx.fragment.app.Fragment
import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadListener
import com.liulishuo.filedownloader.FileDownloader


object FileDownLoadUtils {

    /**
     * 初始化
     */
    fun setup(context:Context){
        FileDownloader.setup(context)
    }

    /**
     * 下载文件
     */
    fun downloadFile(url:String,
                      path:String,
                      getProgress:(totalBytes:Int,soFarBytes:Int,progress:Int) -> Unit,
                      onCompleted:() -> Unit,
                      onError:()-> Unit) : BaseDownloadTask {
         return FileDownloader.getImpl()
             .create(url)
             .setPath(path)
             .setListener(object : FileDownloadListener() {
                override fun warn(task: BaseDownloadTask) {
                    //如果存在了相同的任务，那么就继续下载

                }

                override fun completed(task: BaseDownloadTask) {
                    val totalBytes = task.smallFileTotalBytes
                    val soFarBytes = task.smallFileSoFarBytes
                    getProgress(
                        totalBytes,
                        soFarBytes,
                        getDownloadProgress(soFarBytes, totalBytes).toInt()
                    )
                    onCompleted()
                }

                override fun pending(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    getProgress(
                        totalBytes,
                        soFarBytes,
                        getDownloadProgress(soFarBytes, totalBytes).toInt()
                    )
                }

                override fun error(task: BaseDownloadTask, e: Throwable?) {
                    onError()
                }

                override fun progress(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    getProgress(
                        totalBytes,
                        soFarBytes,
                        getDownloadProgress(soFarBytes, totalBytes).toInt()
                    )
                }

                override fun paused(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    getProgress(
                        totalBytes,
                        soFarBytes,
                        getDownloadProgress(soFarBytes, totalBytes).toInt()
                    )
                }

            })
    }


    fun getDownloadProgress(soFarBytes: Int, totalBytes: Int) : Double{
        return ((soFarBytes.toDouble().div(totalBytes.toDouble())).times(100))
    }

    fun pause(downloadId:Int){
        FileDownloader.getImpl().pause(downloadId)
    }

}

/**
 * 下载文件
 */
inline fun Context.downloadFile(url:String,
                                 path:String,
                                 tag:Any ? = null,
                                 crossinline getProgress:(totalBytes:Int,soFarBytes:Int,progress:Int) -> Unit,
                                 crossinline onCompleted:() -> Unit,
                                 crossinline onError:()-> Unit) : BaseDownloadTask{
    val task =  FileDownLoadUtils.downloadFile(url, path,
        getProgress={totalBytes,soFarBytes,progress ->
                    getProgress(totalBytes, soFarBytes, progress)
        },
        onCompleted={onCompleted()},
        onError={onError()})

    if(tag != null){
        task.setTag(tag)
    }
    return task
}

/**
 * 下载文件
 */
inline fun Fragment.downloadFile(url:String,
                                 path:String,
                                 tag:Any ? = null,
                                 crossinline getProgress:(totalBytes:Int,soFarBytes:Int,progress:Int) -> Unit,
                                 crossinline onCompleted:() -> Unit,
                                 crossinline onError:()-> Unit) : BaseDownloadTask?{
    return this.activity?.downloadFile(url, path,tag, getProgress, onCompleted, onError)?:null
}



